/********** Library Includes **************************************************/

#include <opencv/cv.h>
#include <opencv/highgui.h>

/********** Local Includes ****************************************************/

#include "common.h"
#include "cannedsource.h"

/********** Macros and Constants **********************************************/

#define HUE 0
#define SAT 1
#define VAL 2

/********** Namespaces ********************************************************/

using namespace cv;
using namespace std;

/********** Local Functions ***************************************************/

Mat image;

void on_mouse(int event, int x, int y, int flags, void *)
{
	switch(event) {
		case CV_EVENT_LBUTTONDOWN:
			Vec3b colors = image.at<Vec3b>(y, x);
			
			cout << "x: " << x << endl;
			cout << "y: " << y << endl;
			cout << "Hue:        " << int(colors[HUE]) << endl;
			cout << "Saturation: " << int(colors[SAT]) << endl;
			cout << "Value:      " << int(colors[VAL]) << endl;
			
			break;
	}
}

int main(int argc, char **argv) {
	if (argc < 2) {
		cerr << "Usage: " << argv[0] << " filename" << endl;
		return 1;
	}
	
	CannedSource *source = new CannedSource();
	source->setImageFilename(argv[1]);
	
	image = source->next();
	imshow("Image", image);
	cvtColor(image, image, CV_BGR2HSV);
	cvSetMouseCallback("Image", on_mouse, 0);
	
	cout << "Press 'q' to close" << endl;
	
	while (cvWaitKey(300) != 'q');
}